<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form :model="ruleForm" ref="ruleForm" label-width="120px" class="eidt-el-form">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="ruleForm.remark"></el-input>
            </el-form-item>
            <el-form-item label="生成电子票数量" prop="number">
              <el-input v-model="ruleForm.number" min='1' type="number"></el-input>
            </el-form-item>
            <el-form-item label="票类型" prop="ticketTypeId">
              <el-select v-model="ruleForm.ticketTypeId" prop="ticketTypeId" placeholder="选择票类型">
                <el-option
                  v-for="(type,index) in TicketType"
                  :label="type.name"
                  :key="index"
                  :value="type.ticketTypeId">
                  <div @click="getChangeType(type)">{{type.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>
<script>
import { Loading } from 'element-ui';
export default {
  data() {
    return {
      rules: {
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        ticketTypeId: [{ required: true, message: "票类型不能为空", trigger: "blur" }],
        number: [{ required: true, message: "数量不能为空", trigger: "blur" }]
      }, 
      ruleForm: {
        ticketTypeId:"",
        remark: "",
        number:"",
        typeName:"",
        type:3,
        level:0,
        projectId:localStorage.getItem('project_Id'),
        phone:localStorage.getItem("ms_username"),
        name :localStorage.getItem("name"),
        accountId:sessionStorage.getItem('accountId'),
      },
      ticketCom_Id: "",
      TicketType:[]
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created(){ 
    this.getTicketType();
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm = item;
      this.ticketCom_Id=item.ticketRecordId;
    }
  },
  methods: { 
    saveClick(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.ticketCom_Id == '' || this.ticketCom_Id == undefined) {
          const loading =this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
            this.$axios.post("/TicketRecord/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
              } else {
                this.$message.error("保存失败！");
              }
              
              loading.close();
            });
          } else {
            this.$axios.put("/TicketRecord/" + this.ticketCom_Id + "", this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.ticketCom_Id = '';
      this.$refs['ruleForm'].resetFields();
    },
    /*getUploadImg(imgSrc){
      this.ruleForm.banner = imgSrc;
    },*/
    getTicketType(){
      this.$axios.get('/TicketType/Project/'+this.ruleForm.projectId).then(res =>{
        if (res.data.hasError == false) {
          this.TicketType = [...res.data.data]
        }
      })
    },
    getChangeType(type){
      this.ruleForm.ticketTypeId = type.ticketTypeId;
      this.ruleForm.typeName = type.name;
    }
  }
};
</script>